package com.example.imgup;
import android.content.Context;
import android.os.Environment;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
public class ExportLabelsToFile {
    public static void exportLabelsToFile(List<String> sortedLabels, Context context) {
        String savePath = context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();
        String fileName = "classes";
        // 创建要保存的文件
        File directory = new File(savePath);
        if (!directory.exists()) {
            directory.mkdirs();
        }

        File file = new File(directory, fileName + ".txt");
        try {
            // 创建文件输出流
            FileOutputStream fos = new FileOutputStream(file);
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos));

            // 将标签写入文件
            for (String label : sortedLabels) {
                writer.write(label); // 写入标签文字
                writer.newLine(); // 换行，每个标签占一行
            }

            // 关闭流
            writer.close();
            fos.close();

            // 如果需要，显示成功保存的消息
            Toast.makeText(context, "标签已导出到 " + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
            // 如果保存失败，显示错误消息
            Toast.makeText(context, "导出标签失败", Toast.LENGTH_SHORT).show();
        }
    }
}
